package com.jy.adscm.operation.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.base.bean.TaskManager;
import com.jy.adscm.operation.base.bean.TaskManagerdetail;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerMapper;
import com.jy.adscm.operation.base.mapper.mysql.TaskManagerdetailMapper;
import com.jy.adscm.operation.base.service.TaskManagerService;
import com.jy.adscm.operation.base.service.TaskManagerdetailService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.utils.AdscmDateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-12-11
 */
@Service
public class TaskManagerdetailServiceImpl extends ServiceImpl<TaskManagerdetailMapper, TaskManagerdetail> implements TaskManagerdetailService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    TaskManagerMapper taskManagerMapper;
    @Autowired
    TaskManagerService TaskManagerService;
    @Override
    public Boolean updateTaskManagerdetail(TaskManagerdetail taskManagerdetail, Token token) throws AdscmSystemException {
        if (taskManagerdetail.getCreator().equals(token.getUser().getId())){
            TaskManagerService.updateByPerformer(taskManagerdetail.getTaskId());
        }else {
            TaskManagerService.updateByCreator(taskManagerdetail.getTaskId());
        }
        TaskManager taskManager = taskManagerMapper.selectById(taskManagerdetail.getTaskId());
        if (taskManager.getTaskState()==1 && "0".equals(taskManager.getIsEveryDayTask())){
            throw  new AdscmSystemException("任务已经结束了");
         }
        boolean b =false;
        TaskManagerdetail oldTaskManagerdetail = super.selectById(taskManagerdetail.getId());
        if (oldTaskManagerdetail.getCreator().equals(token.getUser().getId().toString())){
            taskManagerdetail.setManager(null);
            taskManagerdetail.setManagerAdvice(null);
            taskManagerdetail.setAdviceCreattime(null);
            b= super.updateById(taskManagerdetail);
        }  else {
            taskManagerdetail.setCreatTime(null);
            taskManagerdetail.setCreator(null);
            taskManagerdetail.setStartDate(null);
            taskManagerdetail.setEndDate(null);
            taskManagerdetail.setTaskDescription(null);
            taskManagerdetail.setManager(token.getUser().getId().toString());
            taskManagerdetail.setAdviceCreattime(new Date());
            b=super.updateById(taskManagerdetail);

        }

        return b;
    }

    @Override
    public List<TaskManagerdetail> selectByTid(String taskId) {
        Wrapper<TaskManagerdetail> wrapper =new EntityWrapper<>();
        wrapper.eq("task_id",taskId);
        wrapper.orderBy("creatTime",false);
        List<TaskManagerdetail> TaskManagerdetails = super.selectList(wrapper);
        for (TaskManagerdetail taskManagerdetail : TaskManagerdetails) {
             if (AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getManager(),null)){
                 JyUser jyUser = jyUserMapper.selectById(taskManagerdetail.getManager());
                 taskManagerdetail.setManagerName(jyUser.getName());
             }
        }
        return TaskManagerdetails;
    }

    @Override
    public boolean insertTaskManagerdetail(TaskManagerdetail taskManagerdetail, Token token) throws AdscmSystemException{
         TaskManagerService.updateByPerformer(taskManagerdetail.getTaskId());
        TaskManager taskManager = taskManagerMapper.selectById(taskManagerdetail.getTaskId());
        if ( "0".equals(taskManager.getIsEveryDayTask())){
              if (!AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getStartDate(),null)){
                  throw  new AdscmSystemException("任务没有开始，请填写开始时间");
              }else {
                  if (AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getEndDate(),null)&&
                          AdscmBaseUtils.getInstance().isNotEmpty(taskManager.getStartDate(),null)){
                       throw  new AdscmSystemException("任务已结束");
                  }

              }
        }
        if(AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getEndDate(),null)){
            if (!AdscmBaseUtils.getInstance().isNotEmpty(taskManagerdetail.getStartDate(),null)){
                throw new AdscmSystemException("没填写开始时间");
            }
        }
        taskManagerdetail.setCreator(token.getUser().getId().toString());
        taskManagerdetail.setCreatTime(new Date());

        super.insert(taskManagerdetail);
        return false;
    }
}
